package com.example.client_text.clientUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.client_text.R;
import com.google.android.material.textfield.TextInputEditText;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class SocketClient extends AppCompatActivity implements Runnable {
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 12345;
    //定义相关变量,完成初始化
    private TextView tv_status, tv_show;
    private TextInputEditText tv_ip, tv_port, tv_send;
    private Button btn_send;
    private Socket socket = null;
    private BufferedReader br = null;
    private DataOutputStream dos = null;
    private DataInputStream dis = null;
    private String content = "";
    private StringBuilder sb = null;
    private boolean f = true;
    private boolean isSend = false;


    //定义一个handler对象,用来刷新界面
    public Handler handler = new Handler(Looper.myLooper()) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x000:
                    f = false;
                    break;
                case 0x001:
                    tv_status.setText("发送消息不能为空");
                    f = false;
                    break;
                case 0x111:
                    tv_status.setText("连接断开,消息无法获取");
                    f = false;
                    break;
                case 0x123:
                    sb.append(content);
                    tv_show.setText(sb.toString());
                    break;
                case 0x003:
                    tv_status.setText("连接失败");
                    f = false;
                    break;
                case 0x404:
                    tv_status.setText("发送失败");
                    f = false;
                    break;
                case 0x666:
                    tv_status.setText("连接成功");
                    isSend=true;
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb = new StringBuilder();
        tv_ip = findViewById(R.id.tv_ip);
        tv_port = findViewById(R.id.tv_port);
        tv_send = findViewById(R.id.tv_send);
        tv_show = findViewById(R.id.tv_msg);
        tv_status = findViewById(R.id.tv_status);
        btn_send = findViewById(R.id.btn_send);
        new Thread(this::run).start();
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(isSend){
                            if(tv_send.getText()==null||String.valueOf(tv_send.getText()).equals("")){
                                handler.sendEmptyMessage(0x001);
                                return;
                            }
                            send(String.valueOf(tv_send.getText()));
                            handler.sendEmptyMessage(0x666);
                        }else {
                            handler.sendEmptyMessage(0x404);
                        }

                    }
                }).start();
            }
        });
    }
    private void send(String mgs) {
        try {
            dos.writeUTF(mgs);
        } catch (IOException e) {
           CloseUtil.CloseAll(dos);
           handler.sendEmptyMessage(0x404);
        }
    }

    @Override
    public void run() {
        try {
            socket = new Socket(HOST, PORT);
            dos = new DataOutputStream(socket.getOutputStream());
            dis = new DataInputStream(socket.getInputStream());
//            dos.writeUTF("安卓客户端连接成功 ");
            if(socket.isConnected()){
                handler.sendEmptyMessage(0x666);
            }
        } catch (IOException e) {
            CloseUtil.CloseAll(socket);
            f = false;
            handler.sendEmptyMessage(0x000);
        }

        while (f) {
            try {
                content=dis.readUTF();
                content += "\n";
                handler.sendEmptyMessage(0x123);

            } catch (Exception e) {
                f = false;
                CloseUtil.CloseAll(socket);
                handler.sendEmptyMessage(0x111);
            }
        }

    }
}





